package com.peko.redis.utils;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

/**
 * @author Peko
 * 用来获取SpringBoot创建好的工厂
 */

@Component
public class ApplicationContextUtils implements ApplicationContextAware {

    private static ApplicationContext applicationContext;

    /**
     * 将创建好的工厂以参数的形式传递给这个类
     * @param applicationContext1
     * @throws BeansException
     */
    @Override
    public void setApplicationContext(ApplicationContext applicationContext1) throws BeansException {
        //保留创建好的工厂
        applicationContext = applicationContext1;
    }

    /**
     * 提供在工厂中获取对象的方法
     */
    public static Object getBean(String beanName){
        return applicationContext.getBean(beanName);
    }
}
